using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Inventory : MonoBehaviour
{
    public GameObject inventoryPanel;
    public Transform dropPosition;
    public ItemSlotUI[] uiSlots;
    public ItemSlot[] slots;
    [Header("Selecting Items")]
    private ItemSlot selectedItem;
    private int selectedItemIndex;
    public Text selectedItemName, selectedItemDesc, selectedItemstatName, selectedItemsStatValue;
    public GameObject useBtn/*, equipBtn, unEquipBtn*/, dropBtn;
    private int currentEquipIndex;
    public GameObject healthNameObj;

    public static Inventory instance;

    private void Awake()
    {
        instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        inventoryPanel.SetActive(false);
        slots = new ItemSlot[uiSlots.Length];

        for (int i = 0; i < slots.Length; i++)
        {
            slots[i] = new ItemSlot();
            uiSlots[i].index = i;
            uiSlots[i].Clear();
        }
        ClearSelectedItemWindow();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.I)) { OpenInventory(); }
    }

    public void AddItem(ItemData item)
    {
        if (item.canStack)
        {
            OpenInventory();
            ItemSlot slotToStackTo = GetItemStack(item);
            if (slotToStackTo != null)
            {
                slotToStackTo.quantity++;
                UpdateUI();
                return;
            }
        }
        ItemSlot emptySlot = GetEmptySlot();
        if (emptySlot != null)
        {
            OpenInventory();
            emptySlot.item = item;
            emptySlot.quantity = 1;
            UpdateUI();
            return;
        }
        ThrowItem(item);
    }
    public void ThrowItem(ItemData item)
    {
        Instantiate(item.dropPrefab, dropPosition.position, dropPosition.rotation);
    }

    void UpdateUI()
    {
        for (int x = 0; x < slots.Length; x++)
        {
            if (slots[x].item != null)
            {
                uiSlots[x].Set(slots[x]);
            }
            else
            {
                uiSlots[x].Clear();
            }
        }
    }

    ItemSlot GetItemStack(ItemData item)
    {
        for (int x = 0; x < slots.Length; x++)
        {
            if (slots[x].item!=null&&slots[x].item.name == item .name&& slots[x].quantity < item.maxStackAmount)
            {
                return slots[x];
            }
        }
        return null;
    }

    ItemSlot GetEmptySlot()
    {
        for (int i = 0; i < slots.Length; i++)
        {
            if (slots[i].item == null)
            {
                return slots[i];
            }
        }
        return null;
    }

    public void SelectItem(int index)
    {
        if (slots[index].item == null)
            return;
        selectedItem = slots[index];
        selectedItemIndex = index;

        selectedItemName.text = selectedItem.item.displayName;
        selectedItemDesc.text = selectedItem.item.description;

        selectedItemsStatValue.text = string.Empty;
        selectedItemstatName.text = string.Empty;

        for (int x = 0; x < selectedItem.item.consumables.Length; x++)
        {
            selectedItemstatName.text += selectedItem.item.consumables[x].type.ToString() + "\n";
            selectedItemsStatValue.text += selectedItem.item.consumables[x].value.ToString() + "\n";
        }
        useBtn.SetActive(selectedItem.item.type == ItemType.Consumable);
        //equipBtn.SetActive(selectedItem.item.type == ItemType.Equipable && !uiSlots[index].equipped);
        //unEquipBtn.SetActive(selectedItem.item.type == ItemType.Equipable && uiSlots[index].equipped);
        dropBtn.SetActive(true);
        healthNameObj.SetActive(true);
    }

    void ClearSelectedItemWindow()
    {
        selectedItem = null;
        selectedItemName.text = string.Empty;
        selectedItemDesc.text = string.Empty;
        selectedItemsStatValue.text = string.Empty;
        healthNameObj.SetActive(false);
        useBtn.SetActive(false);
        dropBtn.SetActive(false);
    }
    public void OnUseBtn()
    {
        if (selectedItem.item.type == ItemType.Consumable)
        {
            for (int x = 0; x < selectedItem.item.consumables.Length; x++)
            {
                switch (selectedItem.item.consumables[x].type)
                {
                    case ConsumableType.Health:
                        HealthManager.Instance.AddHealth(selectedItem.item.consumables[x].value);
                        break;
                }
            }
        }
        RemoveSelectedItem();
    }

    public void OnDropBtn()
    {
        ThrowItem(selectedItem.item);
        RemoveSelectedItem();
    }

    void RemoveSelectedItem()
    {
        selectedItem.quantity -= 1;
        if (selectedItem.quantity == 0)
        {
            //if (uiSlots[selectedItemIndex].equipped==true)
            //{
            //    UnEquip(selectedItemIndex);
            //}
            selectedItem.item = null;
            ClearSelectedItemWindow();
        }
        UpdateUI();
    }

    public void RemoveItem(ItemData item)
    {


    }

    public bool HasItem(ItemData item, int quatity)
    {
        return false;
    }

    public void OpenInventory()
    {
        inventoryPanel.SetActive(true);
        Time.timeScale = 0;
        PlayerCon.isPause = true;
        ClearSelectedItemWindow();
    }
    public void CloseInventory()
    {
        PlayerCon.isPause = false;
        Time.timeScale = 1;
        inventoryPanel.SetActive(false);
    }
}

public class ItemSlot
{
    public ItemData item;
    public int quantity;
}
